﻿Imports System.Data.OleDb

Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        'Get the most recent 5 recipes from the RECIPES table so as to populate the 5 images in the banner

        Dim bannerDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
        Dim bannerSqlString As String = "SELECT * from RECIPE ORDER BY REC_ID DESC"
        Dim bcmd As OleDbCommand = New OleDbCommand(bannerSqlString, bannerDbConn)
        bcmd.CommandType = CommandType.Text
        bannerDbConn.Open()

        Dim pdr As OleDbDataReader = bcmd.ExecuteReader
        Dim longImagePath As String = ""
        Dim longRecName As String = ""
        Dim longIDPath As String = ""
        Dim cnt As Integer = 0

        'Read the image paths of the first 5 records and save them in the same string separated by a comma 
        While (pdr.Read() And cnt < 5)
            longImagePath += (pdr("REC_IMAGE") & ",")
            longRecName += (pdr("REC_NAME") & ",")
            longIDPath += (pdr("REC_ID") & ",")
            cnt += 1
        End While

        'Separate the strings into arrays
        Dim separatePaths = longImagePath.Split(",")
        Dim separateNames = longRecName.Split(",")
        Dim separateIDs = longIDPath.Split(",")

        'Reference each index and set the ImageUrl properties
        bnrImage1.ImageUrl = separatePaths(0)
        bnrImage2.ImageUrl = separatePaths(1)
        bnrImage3.ImageUrl = separatePaths(2)
        bnrImage4.ImageUrl = separatePaths(3)
        bnrImage5.ImageUrl = separatePaths(4)

        'Reference each index and set the ToolTip (image title) properties
        bnrImage1.ToolTip = separateNames(0)
        bnrImage2.ToolTip = separateNames(1)
        bnrImage3.ToolTip = separateNames(2)
        bnrImage4.ToolTip = separateNames(3)
        bnrImage5.ToolTip = separateNames(4)

        'Reference each index and set the NavigateURL property of each ASP hyperlink wrapping each banner
        hypBnrImage1.NavigateUrl = "/RecipeDetail.aspx?ID=" & separateIDs(0)
        hypBnrImage2.NavigateUrl = "/RecipeDetail.aspx?ID=" & separateIDs(1)
        hypBnrImage3.NavigateUrl = "/RecipeDetail.aspx?ID=" & separateIDs(2)
        hypBnrImage4.NavigateUrl = "/RecipeDetail.aspx?ID=" & separateIDs(3)
        hypBnrImage5.NavigateUrl = "/RecipeDetail.aspx?ID=" & separateIDs(4)

    End Sub

    Public Function getBaconStars(ByVal stars As Integer)
        'Given the number of stars, this function returns a string with the path to the relative image
        Dim retval As String = ""
        Select Case stars
            Case -1 To -0.99
                retval = "ui/0Bacons.jpg"
            Case 0 To 0.99
                retval = "ui/1Bacons.jpg"
            Case 1 To 1.99
                retval = "ui/2Bacons.jpg"
            Case 2 To 2.99
                retval = "ui/3Bacons.jpg"
            Case 3 To 3.99
                retval = "ui/4Bacons.jpg"
            Case 4 To 4.99
                retval = "ui/5Bacons.jpg"

        End Select
        Return retval
    End Function

End Class